var subscribers = [];
var nil = function(){};
var PADDED = ' pad';
var NOPAD = '';

function IncludeJavaScript(jsFile)
{
	document.write('<script type="text/javascript" src="' + jsFile + '"></script>'); 
}

IncludeJavaScript('xsheet-nametab.py');
IncludeJavaScript('xsheet-combattab.py');
IncludeJavaScript('xsheet-skilltab.py');
/*IncludeJavaScript('xsheet-itemstab.js');
IncludeJavaScript('xsheet-magictab.js');
IncludeJavaScript('xsheet-minionstab.js');*/

function createLabeledField(id, toplabel, leftlabel, size, editable, padding)
{   
    var inp = document.createElement('input');
        inp.id = id; inp.size = size;
        if(!editable) { inp.disabled=true; /*inp.readonly=true;*/ }
        initPublisher(inp);

    var spn = document.createElement('span');
        spn.id = id + '_lbl';
        spn.innerHTML = toplabel;

    var ret = document.createElement('div');
        ret.setAttribute('class','inbl' + padding);
        ret.appendChild(spn);
        ret.appendChild(document.createElement('br'));
        ret.appendChild(document.createTextNode(leftlabel));
        ret.appendChild(inp);
        
    return ret;
}

function appendLabeledField(p, id, toplabel, leftlabel, size, editable, padding)
{
    var f = createLabeledField(id, toplabel, leftlabel, size, editable, padding);
	p.appendChild(f);
}

function createCaptionedField(id, caption, leftlabel, size, editable, padding)
{
    var inp = document.createElement('input');
        inp.id = id; inp.size = size;
        if(!editable) { inp.disabled=true; /*inp.readonly=true;*/ }
        initPublisher(inp);

    var cap = document.createElement('span');
        cap.id = id + '_lbl';
        cap.setAttribute('class', 'sub'); cap.innerHTML = caption;
    
    var ret = document.createElement('div');
        ret.setAttribute('class','inbl' + padding);
        ret.appendChild(document.createElement('br'));
        ret.appendChild(document.createTextNode(leftlabel));
        ret.appendChild(inp);
        ret.appendChild(document.createElement('br'));
        ret.appendChild(cap);
    
    return ret;
}

function appendCaptionedField(p, id, caption, leftlabel, size, editable, padding)
{
    var f = createCaptionedField(id, caption, leftlabel, size, editable, padding);
    p.appendChild(f);
}

function createConnector(char, padding)
{
    var ret = document.createElement('div');
		ret.setAttribute('class','inbl down' + padding);
		ret.innerHTML = '<br />' + char;
    return ret;
}

function appendConnector(p, char, padding)
{
    var f = createConnector(char, padding);
    p.appendChild(f);
}

function createListEnder()
{
    var ret = document.createElement('br');
		ret.setAttribute('class','endls');
		ret.setAttribute('id','listend');
    return ret;
}

function appendListEnder(p)
{
    var f = createListEnder();
    p.appendChild(f);
}

function deleteFromContaining(curr)
{ curr.parentNode.removeChild(curr); }

function createActionBlank(id, lbl)
{
    var ret = document.createElement('a');
        ret.innerHTML = lbl;
        ret.href = '#';
        ret.id = id;
    return ret;
}

function initPublisher(elem)
{ elem.notify = nil; }

function recalcAll()
{
    for (p in subscribers) {
        subscribers[p](); }
}

function zInt(intstr)
{
	if(intstr && intstr != '') {
		return parseInt(intstr); }
	return 0;
}

function constructAllTabs()
{
    constructNameTab();
    constructCombatTab();
    constructSkillTab();
  /*constructItemsTab();
    constructMagicTab();
    constructMinionsTab();*/
    
    populateAllTabs();
}

function populateAllTabs()
{
    populateNameTab();
    populateCombatTab();
    populateSkillTab();
  /*populateItemsTab();
    populateMagicTab();
    populateMinionsTab();*/
}

function setValueById(id, value)
{ document.getElementById(id).value = value; }
